Ext.define('Roy.controller.base.FishController', {
    extend: 'Roy.controller.base.Controller',
	
	views : [
		'base.Window'
	],
	addFormConfig : undefined,
	
	initBaseControl : function(){
	},
	
	onQuery : function(){
		var view = this.getEwayView();
		var form = view.down('form').getForm();
		var bool = form.isValid(); 
		
		// 查询输入验证
		if (bool == false) {
			Ext.Msg.alert("提示","查询项中存在不合法的输入，不能提交");
			return
		}
		var values = form.getValues();
		var store = view.down('gridpanel').getStore();
		store.setUrlParamsByObject(values);
		store.loadPage(1);
	},
	
	onAdd : function(){
		this._onAddOrUpdate('add');
	},
	onUpdate : function(){
		this._onAddOrUpdate('update');
	},
	onRemove : function(){
		var me = this;
		var view = this.getEwayView(),
			grid = view.getComponent('gridItemId'),
			store = grid.getStore(),
			sm = grid.getSelectionModel(),
			count = sm.getCount(),
			params = store.getUrlParams();
		if(count == 0){
			Ext.Msg.alert("提示", "请选择您要删除的记录！");
			return;
		}
		else if(count > 1){
			Ext.Msg.alert("提示", "一次只能删除一条记录！");
			return;
		}
		Ext.MessageBox.confirm("请确认", "是否真的要删除指定的记录？",function(button,text){
			if(button == 'yes'){
				var record = sm.getLastSelected();
				var params = store.getUrlParams();
				var index = store.indexOf(record);
				store.cleanUrlParam();
				store.remove(record);
				store.sync({
					callback : function(batch,option){
						var response = batch.operations[0].response;
						var object = Ext.decode(response.responseText);
						if(object.hasOwnProperty('errorMsg')){
							store.insert(index,[record]);
							store.commitChanges();
							Ext.Msg.alert('提示',object.errorMsg);
						}
						else {
							Ext.Msg.alert('提示','操作成功');
						}
					},
					scope : me
				});
				store.setUrlParamsByObject(params);
			}
		});
		
	},
	

	_onAddOrUpdate : function(action){
		var title = action=='add' ? "增加"+this.formConfig.title : "修改"+this.formConfig.title;
		var me = this;
		Ext.require([this.formConfig.form],function(){
			if(action=='update'){
				var view = this.getEwayView(),
					grid = view.getComponent('gridItemId'),
					sm = grid.getSelectionModel(),
					count = sm.getCount();
				if(count == 0){
					Ext.Msg.alert("提示", "请选择您要更改的记录！");
					return;
				}
				else if(count > 1){
					Ext.Msg.alert("提示", "一次只能修改一条记录！");
					return;
				}
			}
			
			var win = Ext.create('Eway.view.base.Window',{
				layout : 'fit',
				title : title,
				maximizable: true,
				width : this.formConfig.width ? this.formConfig.width : 500,
				height : this.formConfig.height ?  this.formConfig.height : 500,
				items : [{
					itemId : 'formItemId',
					xtype : this.formConfig.xtype
				}]
			});
			win.setAction(action);
			win.show();
			win.down('button[action="confirm"]').on('click',me._save,me);
			if(action == 'update'){
				var view = this.getEwayView(),
					grid = view.getComponent('gridItemId'),
					sm = grid.getSelectionModel(),
					form = win.getComponent('formItemId');
				form.loadCusRecord(sm.getLastSelected());
			}
		},this);
	},
	
	_save : function(button){
		var view = this.getEwayView(),
			win = button.up('window'),
			form = win.getComponent('formItemId'),
			grid = view.getComponent('gridItemId'),
			store = grid.getStore(),
			params = store.getUrlParams(),
			action = win.getAction(),
			record,backupRecord;
		if(form.getForm().isValid()){
		var me = this;
		var view = this.getEwayView(),
			win = button.up('window'),
			form = win.getComponent('formItemId'),
			grid = view.getComponent('gridItemId'),
			store = grid.getStore(),
			params = store.getUrlParams(),
			action = win.getAction(),
			record,backupRecord;
		store.cleanUrlParam();
		
		// 验证form,,isClassValid(from, action)为后台验证(例子：Device.js)
		/*var bool = form.getForm().isValid() && this.isClassValid(form.getForm(), action);
		if(!bool) {
			// 验证不通过
			return;
		}*/
//		if(form.getForm().isValid()){
			if(action == 'add') {
				var values = form.getCusValues();
				store.insert(0,values);
			}
			else if(action == 'update') {
				record = grid.getSelectionModel().getLastSelected();
//				form.getForm().updateRecord(record);
				form.updateCusRecord(record);
			}
//		}else{
//			Ext.Msg.alert('提示','输入错误！');
//		}
			//如果form中的数据没有改动，则关闭窗口
		if(store.getModifiedRecords().length == 0){
			Ext.Msg.alert('提示','没有更改数据,请更改后再点击确定!');
//			button.up('window').close();
			
		}else{
			store.sync({
			callback : function(batch,option){
				var response = batch.operations[0].response;
				var object = Ext.decode(response.responseText);
				if(object.hasOwnProperty('errorMsg')){
					if(action == 'add'){
						store.removeAt(0);
					}
					else if(action == 'update'){
//						record.reject(true);
					}
					store.commitChanges();
					Ext.Msg.alert('提示',object.errorMsg);
				}else if(object.hasOwnProperty('updateMsg')){
					button.up('window').close();
					Ext.Msg.alert('提示',object.updateMsg);
				}
				else {
					Ext.Msg.alert('提示','操作成功');
					button.up('window').close();
				}
			},
			scope : me
		});
		
		}
		
/*		store.sync({
			callback : function(batch,option){
				var response = batch.operations[0].response;
				var object = Ext.decode(response.responseText);
				if(object.hasOwnProperty('errorMsg')){
					if(action == 'add'){
						store.removeAt(0);
					}
					else if(action == 'update'){
//						record.reject(true);
					}
					store.commitChanges();
					Ext.Msg.alert('提示',object.errorMsg);
				}
				else {
					Ext.Msg.alert('提示','操作成功');
					button.up('window').close();
				}
			},
			scope : me
		});*/
		store.setUrlParamsByObject(params);
		}else{
//			Ext.Msg.alert('提示','输入错误！');
		}
	}
	
});